package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * 银行账户相关
 * Created by MartinKent on 2016/9/13.
 */
public class BankService extends BasicService<JSONArray> {
//    获取银行列表
    public void getBankList(final Callback<BankListResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mBankListResult = null;
        }
        if (null != DataHolder.mBankListResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mBankListResult);
            }
            return;
        }
        String api = "/LmAccountServices-portlet.bank/get-bank";
        invoke(0, api, null, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    DataHolder.mBankListResult = DPGsonUtil.fromJson(result.get(0).toString(), BankListResult.class);
                    if (null != callback) {
                        callback.onSuccess(what, DataHolder.mBankListResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class BankListResult {
        private Status status;

        private List<Value> value;

        String[] countryNames;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(List<Value> value) {
            this.value = value;
        }

        public List<Value> getValue() {
            return this.value;
        }

        public String[] getBankNameArray() {
            if (null == countryNames) {
                countryNames = new String[getValue().size()];
                for (int i = 0; i < countryNames.length; i++) {
                    countryNames[i] = getValue().get(i).getName();
                }
            }
            return countryNames;
        }

        public static class Value {
            private String phone;

            private String bankSwift;

            private String bankRouting;

            private String activeFlag;

            private String description;

            private String name;

            private String bankCd;

            private String postCode;

            private String countryCd;

            private String bankTypeCd;

            private String city;

            private String stateCd;

            private int bankId;

            public void setPhone(String phone) {
                this.phone = phone;
            }

            public String getPhone() {
                return this.phone;
            }

            public void setBankSwift(String bankSwift) {
                this.bankSwift = bankSwift;
            }

            public String getBankSwift() {
                return this.bankSwift;
            }

            public void setBankRouting(String bankRouting) {
                this.bankRouting = bankRouting;
            }

            public String getBankRouting() {
                return this.bankRouting;
            }

            public void setActiveFlag(String activeFlag) {
                this.activeFlag = activeFlag;
            }

            public String getActiveFlag() {
                return this.activeFlag;
            }

            public void setDescription(String description) {
                this.description = description;
            }

            public String getDescription() {
                return this.description;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getName() {
                return this.name;
            }

            public void setBankCd(String bankCd) {
                this.bankCd = bankCd;
            }

            public String getBankCd() {
                return this.bankCd;
            }

            public void setPostCode(String postCode) {
                this.postCode = postCode;
            }

            public String getPostCode() {
                return this.postCode;
            }

            public void setCountryCd(String countryCd) {
                this.countryCd = countryCd;
            }

            public String getCountryCd() {
                return this.countryCd;
            }

            public void setBankTypeCd(String bankTypeCd) {
                this.bankTypeCd = bankTypeCd;
            }

            public String getBankTypeCd() {
                return this.bankTypeCd;
            }

            public void setCity(String city) {
                this.city = city;
            }

            public String getCity() {
                return this.city;
            }

            public void setStateCd(String stateCd) {
                this.stateCd = stateCd;
            }

            public String getStateCd() {
                return this.stateCd;
            }

            public void setBankId(int bankId) {
                this.bankId = bankId;
            }

            public int getBankId() {
                return this.bankId;
            }

        }

        public static class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }
        }
    }
}
